package com.ruoyi.chat.service.impl;

import java.util.*;

import com.ruoyi.chat.domain.ChatGroup;
import com.ruoyi.chat.domain.FriendMessage;
import com.ruoyi.common.core.utils.DateUtils;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.chat.mapper.ChatUserMapper;
import com.ruoyi.chat.domain.ChatUser;
import com.ruoyi.chat.service.IChatUserService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 聊天用户Service业务层处理
 *

 * @date 2023-07-23
 */
@Service
public class ChatUserServiceImpl implements IChatUserService
{
    @Autowired
    private ChatUserMapper chatUserMapper;

    /**
     * 查询聊天用户
     *
     * @param id 聊天用户主键
     * @return 聊天用户
     */
    @Override
    public ChatUser selectChatUserById(Long id)
    {
        return chatUserMapper.selectChatUserById(id);
    }

    /**
     * 查询聊天用户好友信息
     *
     * @param id 聊天用户主键
     * @return 聊天用户
     */
    @Override
    public List<ChatUser> selectChatAllUserById(Long id,Integer friendStatus) {
        return chatUserMapper.selectChatAllUserList(id,friendStatus);
    }

    /**
     * 查询聊天用户列表
     *
     * @param chatUser 聊天用户
     * @return 聊天用户
     */
    @Override
    public List<ChatUser> selectChatUserList(ChatUser chatUser)
    {
        return chatUserMapper.selectChatUserList(chatUser);
    }




    /**
     * 新增聊天用户
     *
     * @param chatUser 聊天用户
     * @return 结果
     */
    @Override
    public int insertChatUser(ChatUser chatUser)
    {
        //        String nickName = chatUser.getNickName();
        UUID uuid = UUID.randomUUID();//生成uuid
        chatUser.setUuid(String.valueOf(uuid));
//        System.out.println(chatUser.toString());
        chatUser.setCreateTime(DateUtils.getNowDate());
        return chatUserMapper.insertChatUser(chatUser);
    }

    /**
     * 修改聊天用户
     *
     * @param chatUser 聊天用户
     * @return 结果
     */
    @Override
    public int updateChatUser(ChatUser chatUser)
    {
        chatUser.setUpdateTime(DateUtils.getNowDate());
        return chatUserMapper.updateChatUser(chatUser);
    }

    /**
     * 批量删除聊天用户
     *
     * @param ids 需要删除的聊天用户主键
     * @return 结果
     */
    @Override
    public int deleteChatUserByIds(Long[] ids)
    {
        return chatUserMapper.deleteChatUserByIds(ids);
    }

    /**
     * 删除聊天用户信息
     *
     * @param id 聊天用户主键
     * @return 结果
     */
    @Override
    public int deleteChatUserById(Long id)
    {
        return chatUserMapper.deleteChatUserById(id);
    }

    /**
     * 删除聊天好友
     *
     * @param id 聊天用户主键
     * @return 结果
     */
    @Override
    public int deleteUserById(Long id,Long userId) {
        return chatUserMapper.deleteUserById(id,userId);
    }

    /**
     * 查询聊天数据（按条数查不重复好友id的最后一个的聊天数据）
     * @param num 获取的条数
     */
    @Override
    public List<FriendMessage> selectAllchatLastOne(Long userId,Integer num) {
//        System.out.println("userId:--------"+userId);
//        Integer page = 0;//偏移量的计算公式为：(页数 - 1) * 每页显示的行数
        List<FriendMessage> friendMessageList = new ArrayList<>();
        Set<Long> userIds = new HashSet<>();//存消息id

        int lineNum = 1;//返回的页数
        int size = 1;//返回的条数
        while (friendMessageList.size()<num){
            int i = (lineNum - 1)*size;
            List<FriendMessage> friendMessageList1 = chatUserMapper.selectChatLastNum(userId,i,size);
//            System.out.println(friendMessageList1.size());
//            System.out.println(friendMessageList.size());
            if(friendMessageList1.size()<1){
                return friendMessageList;//如果没有更多的数据时直接返回
            }
            lineNum++;
            for (FriendMessage friendMessage : friendMessageList1) {
                Long friendId = null;
                if (friendMessage.getUserIdPash().equals(userId)){
                     friendId = friendMessage.getUserIdPull();
                }else {
                    friendId = friendMessage.getUserIdPash();
                }
                if (!userIds.contains(friendId)){
                    userIds.add(friendId);
                    friendMessageList.add(friendMessage);
                }
            }
        }
        return friendMessageList;
    }

    @Override
    public List<ChatGroup> selectChatGroupList(Long userId) {
        return chatUserMapper.selectChatGroupById(userId);
    }




    /**
     *查询单个好友的聊天数据
     */
    @Override
    public List<FriendMessage> selectFriendMessage(Long userId, Long friendId,Integer page, Integer size) {

        List<FriendMessage> friendMessages = chatUserMapper.selectFriendMessage(userId, friendId,(page - 1) * size, size);
        return friendMessages;
    }

    /**
     * 添加好友
     */
    @Override
    @Transactional //不知道这种事务写法是不是对的
    public int addFriend(Long userId, Long friendId, String messageAdd) {
        Date createTime = new Date();
        //由于在查询好友关联时只执行了一边关联，所以为了方便，就增加两行互补
        //先执行当前用户关联好友
        //状态,（同意1、拒绝2、等待验证发起方3、等待验证接收方4）
        Integer friendStatus = 3;
        chatUserMapper.addFriend(userId,friendId,messageAdd,createTime,friendStatus);
        //再执行好友关联当前用户
        Long id = userId;
        userId = friendId;
        friendId = id;
        friendStatus = 4;
        return chatUserMapper.addFriend(userId,friendId,messageAdd,createTime,friendStatus);
    }

    /**
     * 修改好友关系
     * @param userId
     * @param friendId
     * @param friendStatus
     * @return
     */
    @Override
    @Transactional //事务
    public int updateFriendUser(Long userId, Long friendId, Integer friendStatus) {
        Date updateTime = new Date();
        int i = chatUserMapper.updateFriendUser(userId, friendId, updateTime, friendStatus);
        //再执行好友关联当前用户
        Long id = userId;
        userId = friendId;
        friendId = id;
        int i1 = chatUserMapper.updateFriendUser(userId, friendId, updateTime, friendStatus);
        return i+i1;
    }

    @Override
    public int addGroupUser(Long groupId,Long userId,Integer groupStatus) {

        int i = chatUserMapper.insertGroupUser(groupId,userId,groupStatus);
        return i;
    }

    @Override
    public List<ChatUser> selectUserGroupStatus(Long userId) {
        return chatUserMapper.selectUserGroupStatus(userId);
    }



    /**
     * 根据系统id获取聊天用户详细信息
     */
    @Override
    public ChatUser getUserBySystemId(Long systemId) {
        return chatUserMapper.getUserBySystemId(systemId);
    }

    /**
     * 根据群主查询申请群的用户
     */
    @Override
    public List<ChatUser> groupUserList(Long groupId) {
        return chatUserMapper.selectGroupUserList(groupId);
    }


    /**
     * 群主是否同意用户加入群聊
     */
    @Override
    public int updateGroupUsers(Long groupId, Long userId, Integer groupStatus) {
        return chatUserMapper.updateGroupUsers(groupId,userId,groupStatus);
    }
}
